package dao

import (
	"gorm.io/gorm"
	"kf/models/entity"
)

type ReplyDao struct {
	DB *gorm.DB
}

func (replyDao *ReplyDao) FindReplyItemByUserIdTitle(userId any, title string) entity.ReplyItem {
	var reply entity.ReplyItem
	replyDao.DB.Where("user_id = ? and item_name = ?", userId, title).Find(&reply)
	return reply
}
func (replyDao *ReplyDao) FindReplyByUserId(userId any) []*entity.ReplyGroup {
	var replyGroups []*entity.ReplyGroup
	var replyItems []*entity.ReplyItem
	replyDao.DB.Model(&entity.ReplyGroup{}).Where("user_id = ?", userId).Find(&replyGroups)
	replyDao.DB.Model(&entity.ReplyItem{}).Where("user_id = ?", userId).Find(&replyItems)
	temp := make(map[string]*entity.ReplyGroup)
	for _, replyGroup := range replyGroups {
		replyGroup.Items = make([]*entity.ReplyItem, 0)
		temp[replyGroup.Id] = replyGroup
	}
	for _, replyItem := range replyItems {
		temp[replyItem.GroupId].Items = append(temp[replyItem.GroupId].Items, replyItem)
	}
	return replyGroups
}
func (replyDao *ReplyDao) FindReplyTitleByUserId(userId any) []*entity.ReplyGroup {
	var replyGroups []*entity.ReplyGroup
	//DB.Raw("select a.*,b.* from reply_group a left join reply_item b on a.id=b.group_id where a.user_id=? ", userId).Scan(&replyGroups)
	var replyItems []*entity.ReplyItem
	replyDao.DB.Model(&entity.ReplyGroup{}).Where("user_id = ?", userId).Find(&replyGroups)
	replyDao.DB.Model(&entity.ReplyItem{}).Select("item_name,group_id").Where("user_id = ?", userId).Find(&replyItems)
	temp := make(map[string]*entity.ReplyGroup)
	for _, replyGroup := range replyGroups {
		replyGroup.Items = make([]*entity.ReplyItem, 0)
		temp[replyGroup.Id] = replyGroup
	}
	for _, replyItem := range replyItems {
		temp[replyItem.GroupId].Items = append(temp[replyItem.GroupId].Items, replyItem)
	}
	return replyGroups
}
func (replyDao *ReplyDao) CreateReplyGroup(groupName string, userId string) {
	g := &entity.ReplyGroup{
		GroupName: groupName,
		UserId:    userId,
	}
	replyDao.DB.Create(g)
}
func (replyDao *ReplyDao) CreateReplyContent(groupId string, userId string, content, itemName string) {
	g := &entity.ReplyItem{
		GroupId:  groupId,
		UserId:   userId,
		Content:  content,
		ItemName: itemName,
	}
	replyDao.DB.Create(g)
}
func (replyDao *ReplyDao) UpdateReplyContent(id, userId, title, content string) {
	r := &entity.ReplyItem{
		ItemName: title,
		Content:  content,
	}
	replyDao.DB.Model(&entity.ReplyItem{}).Where("user_id = ? and id = ?", userId, id).Updates(r)
}
func (replyDao *ReplyDao) DeleteReplyContent(id string, userId string) {
	replyDao.DB.Where("user_id = ? and id = ?", userId, id).Delete(entity.ReplyItem{})
}
func (replyDao *ReplyDao) DeleteReplyGroup(id string, userId string) {
	replyDao.DB.Where("user_id = ? and id = ?", userId, id).Delete(entity.ReplyGroup{})
	replyDao.DB.Where("user_id = ? and group_id = ?", userId, id).Delete(entity.ReplyItem{})
}
func (replyDao *ReplyDao) FindReplyBySearch(userId any, search string) []*entity.ReplyGroup {
	var replyGroups []*entity.ReplyGroup
	var replyItems []*entity.ReplyItem
	replyDao.DB.Where("user_id = ?", userId).Find(&replyGroups)
	replyDao.DB.Where("user_id = ? and content like ?", userId, "%"+search+"%").Find(&replyItems)
	temp := make(map[string]*entity.ReplyGroup)
	for _, replyGroup := range replyGroups {
		replyGroup.Items = make([]*entity.ReplyItem, 0)
		temp[replyGroup.Id] = replyGroup
	}
	for _, replyItem := range replyItems {
		temp[replyItem.GroupId].Items = append(temp[replyItem.GroupId].Items, replyItem)
	}
	var newReplyGroups = make([]*entity.ReplyGroup, 0)
	for _, replyGroup := range replyGroups {
		if len(replyGroup.Items) != 0 {
			newReplyGroups = append(newReplyGroups, replyGroup)
		}
	}
	return newReplyGroups
}
